﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Jinjia.Approve.Entity.Entity;
using Jinjia.Approve.IRepository;
using Jinjia.Core.UnitOfWork.UnitOfWorkComponent;
using SqlSugar;

namespace Jinjia.Approve.Repository
{
    public class ErpaBaseRepository : IErpaBaseRepository
    {
        private readonly ISqlSugarClient _db;

        public ErpaBaseRepository(IUnitOfWork unitOfWork)
        {
            _db = unitOfWork.GetDbClient();
        }

        public async Task<string> GetValueByCodeType(string code_type)
        {
            return (await GetModelByCodeType(code_type))?.value;
        }

        public async Task<List<string>> GetValuesByCodeType(string code_type)
        {
            return (await GetModelsByCodeType(code_type)).Select(x => x.value).ToList();
        }

        public async Task<erpa_base_tbl> GetModelByCodeType(string code_type)
        {
            return await _db.Queryable<erpa_base_tbl>().Where(x => x.code_type == code_type).FirstAsync();
        }

        public async Task<erpa_base_tbl> GetModelByCodeType(string code_type, string action_flag)
        {
            return await _db.Queryable<erpa_base_tbl>().Where(x => x.code_type == code_type && x.action_flag == action_flag).FirstAsync();
        }

        public async Task<List<erpa_base_tbl>> GetModelsByCodeType(string code_type)
        {
            return await _db.Queryable<erpa_base_tbl>().Where(x => x.code_type == code_type).ToListAsync();
        }
    }
}